Python Algorithmic Trading Cookbook¶

Chapter 4: Candlestick Patterns and Historical Data

This Jupyter Notebook is created using Python version 3.7.2


Recipe 1: Fetching Historical Data using the Broker API

In [1]:
from pyalgotrading.broker.broker_connection_zerodha import BrokerConnectionZerodha
In [2]:
# Get the api_key & api_secret from broker. These are unique to you & will be used by the broker to identify your demat account.
api_key = "<your-api-key>"
api_secret = "<your-api-secret>"
broker_connection = BrokerConnectionZerodha(api_key, api_secret)
https://kite.trade/connect/login?api_key=<your-api-key>
In [3]:
# Get the request token from the above URL
request_token = "<your-api-token>"
broker_connection.set_access_token(request_token)
In [4]:
# Fetch historical data for an instrument
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data = broker_connection.get_historical_data(instrument=instrument, candle_interval='minute', start_date='2020-01-01', end_date='2020-01-01')
historical_data
Out[4]:
timestamp open high low close volume
0 2020-01-01 09:15:00+05:30 473.00 474.45 472.75 473.90 97244
1 2020-01-01 09:16:00+05:30 473.90 474.25 472.85 473.15 104766
2 2020-01-01 09:17:00+05:30 473.20 474.05 473.20 473.50 37920
3 2020-01-01 09:18:00+05:30 473.55 473.80 473.20 473.80 51424
4 2020-01-01 09:19:00+05:30 473.80 474.40 473.70 474.30 45331
5 2020-01-01 09:20:00+05:30 474.40 474.60 474.10 474.45 72955
6 2020-01-01 09:21:00+05:30 474.45 474.45 473.35 473.80 108909
7 2020-01-01 09:22:00+05:30 473.85 474.00 473.50 473.65 47503
8 2020-01-01 09:23:00+05:30 473.65 474.50 473.60 474.50 26431
9 2020-01-01 09:24:00+05:30 474.60 474.75 474.50 474.70 63749
10 2020-01-01 09:25:00+05:30 474.70 475.45 474.65 475.30 90928
11 2020-01-01 09:26:00+05:30 475.10 475.30 474.85 474.85 51991
12 2020-01-01 09:27:00+05:30 474.85 475.10 474.45 474.55 46054
13 2020-01-01 09:28:00+05:30 474.45 475.00 474.45 474.80 27053
14 2020-01-01 09:29:00+05:30 474.75 475.15 474.75 474.85 42753
15 2020-01-01 09:30:00+05:30 475.00 475.05 474.30 474.60 61059
16 2020-01-01 09:31:00+05:30 474.60 474.70 474.40 474.65 23682
17 2020-01-01 09:32:00+05:30 474.55 474.70 474.40 474.40 14957
18 2020-01-01 09:33:00+05:30 474.60 474.60 474.15 474.40 30218
19 2020-01-01 09:34:00+05:30 474.40 474.40 474.05 474.25 18581
20 2020-01-01 09:35:00+05:30 474.10 474.20 473.50 473.65 66475
21 2020-01-01 09:36:00+05:30 473.50 474.25 473.25 474.25 65156
22 2020-01-01 09:37:00+05:30 473.95 474.25 473.95 474.20 19245
23 2020-01-01 09:38:00+05:30 474.20 474.45 474.05 474.25 24827
24 2020-01-01 09:39:00+05:30 474.45 474.50 474.00 474.15 33444
25 2020-01-01 09:40:00+05:30 474.15 474.50 474.00 474.40 14792
26 2020-01-01 09:41:00+05:30 474.45 474.45 474.15 474.30 22511
27 2020-01-01 09:42:00+05:30 474.30 474.35 474.15 474.20 6403
28 2020-01-01 09:43:00+05:30 474.20 474.55 474.20 474.50 21222
29 2020-01-01 09:44:00+05:30 474.50 474.55 474.25 474.30 11867
... ... ... ... ... ... ...
345 2020-01-01 15:00:00+05:30 467.80 468.30 467.70 467.90 44574
346 2020-01-01 15:01:00+05:30 468.05 468.20 467.75 467.85 21741
347 2020-01-01 15:02:00+05:30 467.85 468.20 467.85 468.10 17365
348 2020-01-01 15:03:00+05:30 468.10 468.10 468.00 468.00 11340
349 2020-01-01 15:04:00+05:30 468.05 468.50 468.05 468.45 32557
350 2020-01-01 15:05:00+05:30 468.45 468.85 467.85 468.15 57162
351 2020-01-01 15:06:00+05:30 467.90 468.50 467.90 468.40 21081
352 2020-01-01 15:07:00+05:30 468.40 468.40 468.10 468.35 21599
353 2020-01-01 15:08:00+05:30 468.35 468.40 468.10 468.25 17911
354 2020-01-01 15:09:00+05:30 468.15 468.40 468.10 468.30 16409
355 2020-01-01 15:10:00+05:30 468.10 468.40 468.10 468.15 37860
356 2020-01-01 15:11:00+05:30 468.10 468.20 467.10 467.40 158297
357 2020-01-01 15:12:00+05:30 467.55 467.75 467.30 467.65 56691
358 2020-01-01 15:13:00+05:30 467.55 467.75 467.50 467.70 45489
359 2020-01-01 15:14:00+05:30 467.70 467.90 467.60 467.70 32124
360 2020-01-01 15:15:00+05:30 467.70 468.45 467.45 467.50 160932
361 2020-01-01 15:16:00+05:30 467.50 467.95 466.80 466.80 180612
362 2020-01-01 15:17:00+05:30 466.90 467.70 466.90 467.55 68961
363 2020-01-01 15:18:00+05:30 467.75 467.95 467.30 467.80 63463
364 2020-01-01 15:19:00+05:30 467.75 468.30 467.70 468.00 98865
365 2020-01-01 15:20:00+05:30 467.90 468.20 467.75 468.00 36307
366 2020-01-01 15:21:00+05:30 468.00 468.30 467.20 467.75 76754
367 2020-01-01 15:22:00+05:30 467.75 467.90 467.35 467.65 100912
368 2020-01-01 15:23:00+05:30 467.80 467.85 467.05 467.35 97402
369 2020-01-01 15:24:00+05:30 467.35 467.90 467.35 467.80 20515
370 2020-01-01 15:25:00+05:30 467.80 468.00 467.60 467.85 25907
371 2020-01-01 15:26:00+05:30 467.85 468.00 467.70 467.80 19267
372 2020-01-01 15:27:00+05:30 467.80 467.95 467.55 467.70 30234
373 2020-01-01 15:28:00+05:30 467.70 467.80 467.65 467.65 32464
374 2020-01-01 15:29:00+05:30 467.65 467.75 467.45 467.55 37885

375 rows × 6 columns

In [5]:
historical_data.columns
Out[5]:
Index(['timestamp', 'open', 'high', 'low', 'close', 'volume'], dtype='object')

Recipe 2: Historical Data as Japanese (OHLC) Candlesticks Pattern

In [6]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
In [7]:
# A 'Green' Japanese Candle
candle_green = historical_data.iloc[:1,:]            # Only 1st ROW of historical data
plot_candlestick_chart(candle_green, PlotType.OHLC, "A 'Green' Japanese Candle")
In [8]:
# A Red Japanese Candle
candle_red = historical_data.iloc[1:2,:]            # Only 2nd ROW of historical data
plot_candlestick_chart(candle_red, PlotType.OHLC, "A 'Red' Japanese Candle")
In [9]:
plot_candlestick_chart(historical_data, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 1 Minute')
In [10]:
instrument2 = broker_connection.get_instrument('NSE', 'INFY')
historical_data = broker_connection.get_historical_data(instrument2, 'minute', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:INFY | 1st Jan, 2020 | Candle Interval: 1 Minute')
In [11]:
instrument3 = broker_connection.get_instrument('NSE', 'ICICIBANK')
historical_data = broker_connection.get_historical_data(instrument3, 'minute', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:ICICIBANK | 1st Jan, 2020 | Candle Size: 1 Minute')

Recipe 3: Japanese Candlesticks Pattern with Variation in Candle Intervals

In [12]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
In [13]:
# Fetch an instrument
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
In [14]:
historical_data_1minute = broker_connection.get_historical_data(instrument, 'minute', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data_1minute, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 1 Minute')
In [15]:
historical_data_3minutes = broker_connection.get_historical_data(instrument, '3minute', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data_3minutes, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 3 Minutes')
In [16]:
historical_data_5minutes = broker_connection.get_historical_data(instrument, '5minute', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data_5minutes, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 5 Minutes')
In [17]:
historical_data_10minutes = broker_connection.get_historical_data(instrument, '10minute', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data_10minutes, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 10 Minutes')
In [18]:
historical_data_15minutes = broker_connection.get_historical_data(instrument, '15minute', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data_15minutes, PlotType.OHLC, 'Historical Data | Japanese Candlesticks | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 15 Minutes')
In [19]:
historical_data_30minutes = broker_connection.get_historical_data(instrument, '30minute', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data_30minutes, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 30 Minutes')
In [20]:
historical_data_1hour = broker_connection.get_historical_data(instrument, 'hour', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data_1hour, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 1 Hour')
In [21]:
historical_data_day = broker_connection.get_historical_data(instrument, 'day', '2020-01-01', '2020-01-01')
plot_candlestick_chart(historical_data_day, PlotType.OHLC, 'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: Day')

Recipe 4: Historical Data as Linebreak Candlesticks Pattern

In [22]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
from pyalgotrading.utils.candlesticks.linebreak import Linebreak
In [23]:
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument, 'minute', '2019-12-01', '2019-12-31')
historical_data_1minute_linebreak = Linebreak(historical_data_1minute)
historical_data_1minute_linebreak
Out[23]:
close open timestamp
0 424.00 424.95 2019-12-02 09:15:00+05:30
1 424.50 424.00 2019-12-02 09:16:00+05:30
2 425.75 424.80 2019-12-02 09:17:00+05:30
3 423.75 424.80 2019-12-02 09:19:00+05:30
4 421.70 423.75 2019-12-02 09:20:00+05:30
5 426.65 423.75 2019-12-02 10:00:00+05:30
6 421.40 423.75 2019-12-02 11:06:00+05:30
7 421.10 421.40 2019-12-02 11:09:00+05:30
8 420.85 421.10 2019-12-02 11:11:00+05:30
9 420.75 420.85 2019-12-02 11:12:00+05:30
10 421.50 420.85 2019-12-02 11:15:00+05:30
11 421.75 421.50 2019-12-02 11:17:00+05:30
12 421.85 421.75 2019-12-02 11:18:00+05:30
13 422.60 421.85 2019-12-02 11:20:00+05:30
14 422.75 422.60 2019-12-02 11:23:00+05:30
15 422.80 422.75 2019-12-02 11:24:00+05:30
16 423.25 422.80 2019-12-02 11:28:00+05:30
17 422.05 422.80 2019-12-02 11:32:00+05:30
18 423.30 422.80 2019-12-02 11:36:00+05:30
19 423.45 423.30 2019-12-02 11:37:00+05:30
20 423.90 423.45 2019-12-02 11:46:00+05:30
21 424.00 423.90 2019-12-02 11:48:00+05:30
22 424.10 424.00 2019-12-02 11:50:00+05:30
23 423.05 424.00 2019-12-02 11:57:00+05:30
24 423.00 423.05 2019-12-02 11:58:00+05:30
25 422.65 423.00 2019-12-02 12:01:00+05:30
26 422.50 422.65 2019-12-02 12:02:00+05:30
27 423.20 422.65 2019-12-02 12:08:00+05:30
28 422.45 422.65 2019-12-02 12:11:00+05:30
29 422.30 422.45 2019-12-02 12:12:00+05:30
... ... ... ...
1033 468.30 468.20 2019-12-30 13:05:00+05:30
1034 468.50 468.30 2019-12-30 13:07:00+05:30
1035 468.80 468.50 2019-12-30 13:19:00+05:30
1036 468.00 468.50 2019-12-30 13:27:00+05:30
1037 467.85 468.00 2019-12-30 13:30:00+05:30
1038 470.20 468.00 2019-12-30 13:51:00+05:30
1039 470.40 470.20 2019-12-30 14:03:00+05:30
1040 470.45 470.40 2019-12-30 14:04:00+05:30
1041 470.50 470.45 2019-12-30 14:12:00+05:30
1042 471.25 470.50 2019-12-30 14:14:00+05:30
1043 472.00 471.25 2019-12-30 14:15:00+05:30
1044 472.15 472.00 2019-12-30 14:16:00+05:30
1045 472.75 472.15 2019-12-30 14:17:00+05:30
1046 473.65 472.75 2019-12-30 14:18:00+05:30
1047 473.75 473.65 2019-12-30 14:25:00+05:30
1048 474.65 473.75 2019-12-30 14:26:00+05:30
1049 475.05 474.65 2019-12-30 15:13:00+05:30
1050 475.60 475.05 2019-12-30 15:14:00+05:30
1051 475.85 475.60 2019-12-30 15:15:00+05:30
1052 474.35 475.60 2019-12-30 15:26:00+05:30
1053 474.00 474.35 2019-12-30 15:27:00+05:30
1054 472.50 474.00 2019-12-31 09:15:00+05:30
1055 471.20 472.50 2019-12-31 09:16:00+05:30
1056 474.50 472.50 2019-12-31 10:20:00+05:30
1057 474.55 474.50 2019-12-31 10:43:00+05:30
1058 474.90 474.55 2019-12-31 10:44:00+05:30
1059 471.60 474.55 2019-12-31 11:19:00+05:30
1060 471.50 471.60 2019-12-31 14:19:00+05:30
1061 471.35 471.50 2019-12-31 15:00:00+05:30
1062 471.00 471.35 2019-12-31 15:29:00+05:30

1063 rows × 3 columns

In [24]:
# A 'Green' Linebreak Candle
candle_green_linebreak = historical_data_1minute_linebreak.iloc[1:2,:]            # Only 2nd ROW of historical data
plot_candlestick_chart(candle_green_linebreak, PlotType.LINEBREAK, "A 'Green' Linebreak Candle")
In [25]:
# A Red Linebreak Candle
candle_red_linebreak = historical_data_1minute_linebreak.iloc[:1,:]            # Only 1st ROW of historical data
plot_candlestick_chart(candle_red_linebreak, PlotType.LINEBREAK, "A 'Red' Linebreak Candle")
In [26]:
plot_candlestick_chart(historical_data_1minute_linebreak, PlotType.LINEBREAK, 'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Minute', True)
In [27]:
historical_data_3minutes = broker_connection.get_historical_data(instrument, '3minute', '2019-12-01', '2019-12-31')
historical_data_3minutes_linebreak = Linebreak(historical_data_3minutes)
plot_candlestick_chart(historical_data_3minutes_linebreak, PlotType.LINEBREAK, 'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 3 Minutes', True)
In [28]:
historical_data_5minutes = broker_connection.get_historical_data(instrument, '5minute', '2019-12-01', '2020-01-10')
historical_data_5minutes_linebreak = Linebreak(historical_data_5minutes)
plot_candlestick_chart(historical_data_5minutes_linebreak, PlotType.LINEBREAK, 'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 5 Minutes', True)
In [29]:
historical_data_10minutes = broker_connection.get_historical_data(instrument, '10minute', '2019-12-01', '2020-01-10')
historical_data_10minutes_linebreak = Linebreak(historical_data_10minutes)
plot_candlestick_chart(historical_data_10minutes_linebreak, PlotType.LINEBREAK, 'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 10 Minutes', True)
In [30]:
historical_data_15minutes = broker_connection.get_historical_data(instrument, '15minute', '2019-12-01', '2020-01-10')
historical_data_15minutes_linebreak = Linebreak(historical_data_15minutes)
plot_candlestick_chart(historical_data_15minutes_linebreak, PlotType.LINEBREAK, 'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 15 Minutes', True)
In [31]:
historical_data_30minutes = broker_connection.get_historical_data(instrument, '30minute', '2019-12-01', '2020-01-10')
historical_data_30minutes_linebreak = Linebreak(historical_data_30minutes)
plot_candlestick_chart(historical_data_30minutes_linebreak, PlotType.LINEBREAK, 'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 30 Minutes', True)
In [32]:
historical_data_1hour = broker_connection.get_historical_data(instrument, 'hour', '2019-12-01', '2020-01-10')
historical_data_1hour_linebreak = Linebreak(historical_data_1hour)
plot_candlestick_chart(historical_data_1hour_linebreak, PlotType.LINEBREAK, 'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Hour', True)
In [33]:
historical_data_day = broker_connection.get_historical_data(instrument, 'day', '2019-12-01', '2020-01-10')
historical_data_day_linebreak = Linebreak(historical_data_day)
plot_candlestick_chart(historical_data_day_linebreak, PlotType.LINEBREAK, 'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Day', True)

Recipe 5: Historical Data as Renko Candlesticks Pattern

In [34]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
from pyalgotrading.utils.candlesticks.renko import Renko
In [35]:
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument, 'minute', '2019-12-01', '2020-01-10')
historical_data_1minute_renko = Renko(historical_data_1minute)
historical_data_1minute_renko
Out[35]:
close open timestamp
0 424.0 424.95 2019-12-02 09:15:00+05:30
1 422.0 424.00 2019-12-02 09:20:00+05:30
2 426.0 424.00 2019-12-02 10:00:00+05:30
3 422.0 424.00 2019-12-02 10:12:00+05:30
4 420.0 422.00 2019-12-02 15:28:00+05:30
5 418.0 420.00 2019-12-03 09:15:00+05:30
6 416.0 418.00 2019-12-03 09:17:00+05:30
7 414.0 416.00 2019-12-03 09:20:00+05:30
8 412.0 414.00 2019-12-03 09:35:00+05:30
9 410.0 412.00 2019-12-03 09:38:00+05:30
10 408.0 410.00 2019-12-03 09:52:00+05:30
11 406.0 408.00 2019-12-03 10:16:00+05:30
12 404.0 406.00 2019-12-03 11:22:00+05:30
13 402.0 404.00 2019-12-03 12:54:00+05:30
14 400.0 402.00 2019-12-03 15:11:00+05:30
15 398.0 400.00 2019-12-03 15:12:00+05:30
16 396.0 398.00 2019-12-04 09:15:00+05:30
17 394.0 396.00 2019-12-04 09:15:00+05:30
18 392.0 394.00 2019-12-04 09:15:00+05:30
19 396.0 394.00 2019-12-04 09:48:00+05:30
20 398.0 396.00 2019-12-04 10:13:00+05:30
21 394.0 396.00 2019-12-04 11:02:00+05:30
22 392.0 394.00 2019-12-04 11:09:00+05:30
23 396.0 394.00 2019-12-04 13:51:00+05:30
24 398.0 396.00 2019-12-04 14:36:00+05:30
25 400.0 398.00 2019-12-04 14:37:00+05:30
26 402.0 400.00 2019-12-04 14:38:00+05:30
27 404.0 402.00 2019-12-04 14:38:00+05:30
28 406.0 404.00 2019-12-04 14:41:00+05:30
29 408.0 406.00 2019-12-04 14:43:00+05:30
... ... ... ...
161 474.0 476.00 2020-01-06 11:41:00+05:30
162 472.0 474.00 2020-01-06 14:18:00+05:30
163 476.0 474.00 2020-01-07 09:15:00+05:30
164 478.0 476.00 2020-01-07 09:15:00+05:30
165 480.0 478.00 2020-01-07 09:15:00+05:30
166 482.0 480.00 2020-01-07 09:16:00+05:30
167 484.0 482.00 2020-01-07 09:31:00+05:30
168 480.0 482.00 2020-01-07 11:11:00+05:30
169 478.0 480.00 2020-01-07 13:14:00+05:30
170 476.0 478.00 2020-01-07 13:17:00+05:30
171 474.0 476.00 2020-01-07 13:29:00+05:30
172 472.0 474.00 2020-01-08 09:15:00+05:30
173 470.0 472.00 2020-01-08 11:43:00+05:30
174 468.0 470.00 2020-01-08 12:07:00+05:30
175 472.0 470.00 2020-01-08 13:53:00+05:30
176 474.0 472.00 2020-01-08 14:01:00+05:30
177 476.0 474.00 2020-01-08 15:18:00+05:30
178 478.0 476.00 2020-01-09 09:15:00+05:30
179 480.0 478.00 2020-01-09 09:15:00+05:30
180 482.0 480.00 2020-01-09 09:15:00+05:30
181 484.0 482.00 2020-01-09 09:15:00+05:30
182 480.0 482.00 2020-01-09 12:54:00+05:30
183 484.0 482.00 2020-01-09 14:41:00+05:30
184 486.0 484.00 2020-01-10 10:04:00+05:30
185 488.0 486.00 2020-01-10 10:05:00+05:30
186 490.0 488.00 2020-01-10 10:09:00+05:30
187 492.0 490.00 2020-01-10 11:41:00+05:30
188 488.0 490.00 2020-01-10 13:31:00+05:30
189 486.0 488.00 2020-01-10 13:36:00+05:30
190 484.0 486.00 2020-01-10 14:09:00+05:30

191 rows × 3 columns

In [36]:
# A Green Renko Candle
candle_green_renko = historical_data_1minute_renko.iloc[2:3,:]            # Only 3rd ROW of historical data
plot_candlestick_chart(candle_green_renko, PlotType.RENKO, "A Green 'Renko' Candle")
In [37]:
# A Red Renko Candles
candle_red_renko = historical_data_1minute_renko.iloc[1:2,:]            # Only 2nd ROW of historical data
plot_candlestick_chart(candle_red_renko, PlotType.RENKO, "A 'Red' Renko Candle")
In [38]:
plot_candlestick_chart(historical_data_1minute_renko, PlotType.RENKO, 'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Minute', True)
In [39]:
historical_data_3minutes = broker_connection.get_historical_data(instrument, '3minute', '2019-12-01', '2019-12-31')
historical_data_3minutes_renko = Renko(historical_data_3minutes)
plot_candlestick_chart(historical_data_3minutes_renko, PlotType.RENKO, 'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 3 Minutes', True)
In [40]:
historical_data_5minutes = broker_connection.get_historical_data(instrument, '5minute', '2019-12-01', '2019-12-31')
historical_data_5minutes_renko = Renko(historical_data_5minutes)
plot_candlestick_chart(historical_data_5minutes_renko, PlotType.RENKO, 'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 5 Minutes', True)
In [41]:
historical_data_10minutes = broker_connection.get_historical_data(instrument, '10minute', '2019-12-01', '2019-12-31')
historical_data_10minutes_renko = Renko(historical_data_10minutes)
plot_candlestick_chart(historical_data_10minutes_renko, PlotType.RENKO, 'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 10 Minutes', True)
In [42]:
historical_data_15minutes = broker_connection.get_historical_data(instrument, '15minute', '2019-12-01', '2019-12-31')
historical_data_15minutes_renko = Renko(historical_data_15minutes)
plot_candlestick_chart(historical_data_15minutes_renko, PlotType.RENKO, 'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 15 Minutes', True)
In [43]:
historical_data_30minutes = broker_connection.get_historical_data(instrument, '30minute', '2019-12-01', '2019-12-31')
historical_data_30minutes_renko = Renko(historical_data_30minutes)
plot_candlestick_chart(historical_data_30minutes_renko, PlotType.RENKO, 'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 30 Minutes', True)
In [44]:
historical_data_1hour = broker_connection.get_historical_data(instrument, 'hour', '2019-12-01', '2019-12-31')
historical_data_1hour_renko = Renko(historical_data_1hour)
plot_candlestick_chart(historical_data_1hour_renko, PlotType.RENKO, 'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Hour', True)
In [45]:
historical_data_day = broker_connection.get_historical_data(instrument, 'day', '2019-12-01', '2019-12-31')
historical_data_day_renko = Renko(historical_data_day)
plot_candlestick_chart(historical_data_day_renko, PlotType.RENKO, 'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: Day', True)

Recipe 6: Historical Data as Heikin-Ashi Candlesticks Pattern

In [46]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
from pyalgotrading.utils.candlesticks.heikinashi import HeikinAshi
In [47]:
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument, 'minute', '2019-12-01', '2019-12-31')
historical_data_1minute_heikinashi = HeikinAshi(historical_data_1minute)
historical_data_1minute_heikinashi
Out[47]:
timestamp open high low close volume HA_close HA_open HA_high HA_low
0 2019-12-02 09:15:00+05:30 424.95 424.95 422.55 424.00 135990 424.1125 424.475000 424.950000 422.550000
1 2019-12-02 09:16:00+05:30 424.00 424.90 423.40 424.50 91812 424.2000 424.293750 424.900000 423.400000
2 2019-12-02 09:17:00+05:30 424.80 426.00 424.40 425.75 98656 425.2375 424.246875 426.000000 424.246875
3 2019-12-02 09:18:00+05:30 425.70 425.70 424.50 424.60 49541 425.1250 424.742188 425.700000 424.500000
4 2019-12-02 09:19:00+05:30 424.80 425.20 423.10 423.75 64209 424.2125 424.933594 425.200000 423.100000
5 2019-12-02 09:20:00+05:30 423.95 424.45 421.70 421.70 105442 422.9500 424.573047 424.573047 421.700000
6 2019-12-02 09:21:00+05:30 421.50 422.70 421.45 422.40 65166 422.0125 423.761523 423.761523 421.450000
7 2019-12-02 09:22:00+05:30 422.40 422.45 421.70 421.85 67018 422.1000 422.887012 422.887012 421.700000
8 2019-12-02 09:23:00+05:30 421.65 422.25 421.55 422.25 52946 421.9250 422.493506 422.493506 421.550000
9 2019-12-02 09:24:00+05:30 422.20 423.30 422.15 423.10 33970 422.6875 422.209253 423.300000 422.150000
10 2019-12-02 09:25:00+05:30 423.00 423.75 422.50 423.55 60625 423.2000 422.448376 423.750000 422.448376
11 2019-12-02 09:26:00+05:30 423.90 423.90 423.00 423.10 51538 423.4750 422.824188 423.900000 422.824188
12 2019-12-02 09:27:00+05:30 423.15 423.90 422.80 423.90 32480 423.4375 423.149594 423.900000 422.800000
13 2019-12-02 09:28:00+05:30 423.65 424.80 423.20 424.60 63198 424.0625 423.293547 424.800000 423.200000
14 2019-12-02 09:29:00+05:30 424.75 424.80 424.15 424.40 30227 424.5250 423.678024 424.800000 423.678024
15 2019-12-02 09:30:00+05:30 424.40 424.40 423.25 423.50 23110 423.8875 424.101512 424.400000 423.250000
16 2019-12-02 09:31:00+05:30 423.65 423.85 423.40 423.80 18493 423.6750 423.994506 423.994506 423.400000
17 2019-12-02 09:32:00+05:30 424.00 424.00 423.25 423.35 23947 423.6500 423.834753 424.000000 423.250000
18 2019-12-02 09:33:00+05:30 423.30 423.40 422.80 422.95 21620 423.1125 423.742376 423.742376 422.800000
19 2019-12-02 09:34:00+05:30 423.00 423.75 423.00 423.75 17703 423.3750 423.427438 423.750000 423.000000
20 2019-12-02 09:35:00+05:30 423.85 424.00 423.20 423.45 20284 423.6250 423.401219 424.000000 423.200000
21 2019-12-02 09:36:00+05:30 423.40 423.65 422.90 423.15 22298 423.2750 423.513110 423.650000 422.900000
22 2019-12-02 09:37:00+05:30 423.25 423.25 422.85 423.00 33705 423.0875 423.394055 423.394055 422.850000
23 2019-12-02 09:38:00+05:30 423.00 423.40 422.70 423.00 45118 423.0250 423.240777 423.400000 422.700000
24 2019-12-02 09:39:00+05:30 423.00 424.30 423.00 424.30 27019 423.6500 423.132889 424.300000 423.000000
25 2019-12-02 09:40:00+05:30 424.40 425.65 424.40 424.75 75070 424.8000 423.391444 425.650000 423.391444
26 2019-12-02 09:41:00+05:30 424.95 425.00 424.10 424.10 30498 424.5375 424.095722 425.000000 424.095722
27 2019-12-02 09:42:00+05:30 424.25 424.60 423.50 424.10 38581 424.1125 424.316611 424.600000 423.500000
28 2019-12-02 09:43:00+05:30 424.05 424.50 423.80 424.25 21329 424.1500 424.214556 424.500000 423.800000
29 2019-12-02 09:44:00+05:30 424.40 424.60 424.10 424.30 11568 424.3500 424.182278 424.600000 424.100000
... ... ... ... ... ... ... ... ... ... ...
7845 2019-12-31 15:00:00+05:30 472.00 472.00 471.30 471.35 90159 471.6625 472.252369 472.252369 471.300000
7846 2019-12-31 15:01:00+05:30 471.50 471.50 471.30 471.45 52687 471.4375 471.957434 471.957434 471.300000
7847 2019-12-31 15:02:00+05:30 471.50 472.15 471.35 472.15 19346 471.7875 471.697467 472.150000 471.350000
7848 2019-12-31 15:03:00+05:30 472.15 472.15 471.85 471.85 12139 472.0000 471.742484 472.150000 471.742484
7849 2019-12-31 15:04:00+05:30 471.85 471.95 471.70 471.85 11345 471.8375 471.871242 471.950000 471.700000
7850 2019-12-31 15:05:00+05:30 471.85 471.90 471.70 471.80 27522 471.8125 471.854371 471.900000 471.700000
7851 2019-12-31 15:06:00+05:30 471.80 472.50 471.70 472.25 58453 472.0625 471.833435 472.500000 471.700000
7852 2019-12-31 15:07:00+05:30 472.30 472.30 472.00 472.10 26401 472.1750 471.947968 472.300000 471.947968
7853 2019-12-31 15:08:00+05:30 472.10 472.35 472.10 472.35 33932 472.2250 472.061484 472.350000 472.061484
7854 2019-12-31 15:09:00+05:30 472.45 472.50 472.10 472.10 33029 472.2875 472.143242 472.500000 472.100000
7855 2019-12-31 15:10:00+05:30 472.35 472.50 472.10 472.45 65278 472.3500 472.215371 472.500000 472.100000
7856 2019-12-31 15:11:00+05:30 472.50 472.60 471.90 472.10 92806 472.2750 472.282685 472.600000 471.900000
7857 2019-12-31 15:12:00+05:30 472.25 472.30 472.00 472.10 24459 472.1625 472.278843 472.300000 472.000000
7858 2019-12-31 15:13:00+05:30 472.25 472.30 472.00 472.15 27422 472.1750 472.220671 472.300000 472.000000
7859 2019-12-31 15:14:00+05:30 472.20 472.20 472.00 472.10 31369 472.1250 472.197836 472.200000 472.000000
7860 2019-12-31 15:15:00+05:30 472.10 472.40 471.30 472.15 216638 471.9875 472.161418 472.400000 471.300000
7861 2019-12-31 15:16:00+05:30 472.15 472.45 471.95 472.00 172983 472.1375 472.074459 472.450000 471.950000
7862 2019-12-31 15:17:00+05:30 472.00 472.30 472.00 472.25 52747 472.1375 472.105979 472.300000 472.000000
7863 2019-12-31 15:18:00+05:30 472.40 472.45 472.00 472.30 78973 472.2875 472.121740 472.450000 472.000000
7864 2019-12-31 15:19:00+05:30 472.30 472.40 472.10 472.35 57539 472.2875 472.204620 472.400000 472.100000
7865 2019-12-31 15:20:00+05:30 472.40 472.50 472.15 472.25 47139 472.3250 472.246060 472.500000 472.150000
7866 2019-12-31 15:21:00+05:30 472.45 472.65 471.85 472.40 73411 472.3375 472.285530 472.650000 471.850000
7867 2019-12-31 15:22:00+05:30 472.45 472.45 471.85 472.05 98155 472.2000 472.311515 472.450000 471.850000
7868 2019-12-31 15:23:00+05:30 472.40 472.45 471.45 471.95 80633 472.0625 472.255757 472.450000 471.450000
7869 2019-12-31 15:24:00+05:30 472.00 472.20 471.95 472.00 17485 472.0375 472.159129 472.200000 471.950000
7870 2019-12-31 15:25:00+05:30 472.15 472.20 471.80 471.80 16137 471.9875 472.098314 472.200000 471.800000
7871 2019-12-31 15:26:00+05:30 471.80 471.90 471.60 471.60 25103 471.7250 472.042907 472.042907 471.600000
7872 2019-12-31 15:27:00+05:30 471.75 471.95 471.60 471.80 18560 471.7750 471.883954 471.950000 471.600000
7873 2019-12-31 15:28:00+05:30 471.90 471.95 471.45 471.80 35388 471.7750 471.829477 471.950000 471.450000
7874 2019-12-31 15:29:00+05:30 471.80 471.80 470.65 471.00 42072 471.3125 471.802238 471.802238 470.650000

7875 rows × 10 columns

In [48]:
# A 'Green' HeikinAshi Candle
candle_green_heikinashi = historical_data_1minute_heikinashi.iloc[2:3,:]            # Only 3rd ROW of historical data
plot_candlestick_chart(candle_green_heikinashi, PlotType.HEIKINASHI, "A 'Green' HeikinAshi Candle")
In [49]:
# A 'Red' HeikinAshi Candle
candle_red_heikinashi = historical_data_1minute_heikinashi.iloc[4:5,:]            # Only 1st ROW of historical data
plot_candlestick_chart(candle_red_heikinashi, PlotType.HEIKINASHI, "A 'Red' HeikinAshi Candle")
In [50]:
plot_candlestick_chart(historical_data_1minute_heikinashi, PlotType.HEIKINASHI, 'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 minute', True)
In [51]:
historical_data_3minutes = broker_connection.get_historical_data(instrument, '3minute', '2019-12-01', '2019-12-31')
historical_data_3minutes_heikinashi = HeikinAshi(historical_data_3minutes)
plot_candlestick_chart(historical_data_3minutes_heikinashi, PlotType.HEIKINASHI, 'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 3 minutes', True)
In [52]:
historical_data_5minutes = broker_connection.get_historical_data(instrument, '5minute', '2019-12-01', '2019-12-31')
historical_data_5minutes_heikinashi = HeikinAshi(historical_data_5minutes)
plot_candlestick_chart(historical_data_5minutes_heikinashi, PlotType.HEIKINASHI, 'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 5 minutes', True)
In [53]:
historical_data_10minutes = broker_connection.get_historical_data(instrument, '10minute', '2019-12-01', '2019-12-31')
historical_data_10minutes_heikinashi = HeikinAshi(historical_data_10minutes)
plot_candlestick_chart(historical_data_10minutes_heikinashi, PlotType.HEIKINASHI, 'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 10 minutes', True)
In [54]:
historical_data_15minutes = broker_connection.get_historical_data(instrument, '15minute', '2019-12-01', '2019-12-31')
historical_data_15minutes_heikinashi = HeikinAshi(historical_data_15minutes)
plot_candlestick_chart(historical_data_15minutes_heikinashi, PlotType.HEIKINASHI, 'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 15 minutes', True)
In [55]:
historical_data_30minutes = broker_connection.get_historical_data(instrument, '30minute', '2019-12-01', '2019-12-31')
historical_data_30minutes_heikinashi = HeikinAshi(historical_data_30minutes)
plot_candlestick_chart(historical_data_30minutes_heikinashi, PlotType.HEIKINASHI, 'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 30 minutes', True)
In [56]:
historical_data_1hour = broker_connection.get_historical_data(instrument, 'hour', '2019-12-01', '2019-12-31')
historical_data_1hour_heikinashi = HeikinAshi(historical_data_1hour)
plot_candlestick_chart(historical_data_1hour_heikinashi, PlotType.HEIKINASHI, 'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Hour', True)
In [57]:
historical_data_day = broker_connection.get_historical_data(instrument, 'day', '2019-12-01', '2019-12-31')
historical_data_day_heikinashi = HeikinAshi(historical_data_day)
plot_candlestick_chart(historical_data_day_heikinashi, PlotType.HEIKINASHI, 'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: Day', True)

Recipe 7: Fetch Historical Data using Quandl

In [58]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
import quandl
In [59]:
facebook = quandl.get('WIKI/FB', start_date='2015-1-1', end_date='2015-3-31')
plot_candlestick_chart(facebook, PlotType.QUANDL_OHLC, 'Historical Data | Japanese Candlesticks Pattern | FACEBOOK | Jan-March 2015 | Candle Interval: Day', True)
In [60]:
amazon = quandl.get('WIKI/AMZN', start_date='2015-1-1', end_date='2015-3-31')
plot_candlestick_chart(amazon, PlotType.QUANDL_OHLC, 'Historical Data | Japanese Candlesticks Pattern | AMAZON | Jan-March 2015 | Candle Interval: Day', True)
In [61]:
apple = quandl.get('WIKI/AAPL', start_date='2015-1-1', end_date='2015-3-31')
plot_candlestick_chart(apple, PlotType.QUANDL_OHLC, 'Historical Data | Japanese Candlesticks Pattern | APPLE | Jan-March 2015 | Candle Interval: Day', True)
In [62]:
microsoft = quandl.get('WIKI/MSFT', start_date='2015-1-1', end_date='2015-3-31')
plot_candlestick_chart(microsoft, PlotType.QUANDL_OHLC, 'Historical Data | Japanese Candlesticks Pattern | MICROSOFT | Jan-March 2015 | Candle Interval: Day', True)
In [63]:
google = quandl.get('WIKI/GOOGL', start_date='2015-1-1', end_date='2015-3-31')
plot_candlestick_chart(google, PlotType.QUANDL_OHLC, 'Historical Data | Japanese Candlesticks Pattern | GOOGLE | Jan-March 2015 | Candle Interval: Day', True)
In [ ]: